<?php
namespace Api\Controller;
// +----------------------------------------------------------------------
// | 文件 SsoLoginController.class.php
// +----------------------------------------------------------------------
// | 说明 第三方登录回调地址
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------



use Common\Util\SSO\SaeTOAuthV2;
use Common\Util\SSO\SaeTClientV2;

class SsoLoginController extends PublicController {

	public function qq() {
		print_r(I(''));
	}

	public function sina() {
        // C('URL_MODEL', 2);
        // echo U('Api/SsoLogin/sina','','', true);
        $code = I('code');
		$o = new SaeTOAuthV2('3590771248', '60b08218f8e18ceb8bb0c37d83c04d4d');
        if ($code) {
            $keys = array();
            $keys['code'] = $code;
            $keys['redirect_uri'] = 'http://imba.test.ilongyuan.cn/Api/SsoLogin/sina';
            try {
                $token = $o->getAccessToken( 'code', $keys ) ;
            } catch (\Exception $e) {
                echo $e->getMessage();
                exit;
            }
            $access_token = $token['access_token'];
            $uid = $token['uid'];
             $client = new SaeTClientV2('3590771248', '60b08218f8e18ceb8bb0c37d83c04d4d', $access_token);
            $user_message = $client->show_user_by_id( $uid);//根据ID获取用户等基本信息
            print_r($user_message);
        }
	}

	public function test() {
		$state = 'platform=1'; //CSRF protection
		$login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id="
		. '101300918' . "&redirect_uri=" . urlencode('http://imba.test.ilongyuan.cn')
			. "&state=" . $state
			. "&scope=" . 'get_user_info';
            ///api/Sso/login/channel/qq
		header("Location:$login_url");
	}

	public function testSina() {
		$o = new SaeTOAuthV2('3590771248', '60b08218f8e18ceb8bb0c37d83c04d4d');
		$login_url = $o->getAuthorizeURL('http://imba.test.ilongyuan.cn/api/Sso/login/channel/sina');
        header("Location:$login_url");
	}

}
